/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-10-16
 * Time: 10:47
 */

/**
 * 搜索二叉树
 * 模拟实现增删查改
 */
public class BinarySearchTree {
    class TreeNode {
        public int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    TreeNode root;

    /**
     * 搜索
     * 时间复杂度 ：最好：O(logN)
     *          ：最坏：O(N)单分支
     *
     */
    public boolean search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < key) {
                cur = cur.right;
            } else if (cur.val > key) {
                cur = cur.left;
            } else {
                return true;
            }
        }
        return false;
    }
    /**
     * 插入
     */
    public boolean insert(int val) {
        if(root == null) {
            TreeNode node = new TreeNode(val);
            root = node;
            return true;
        }
        TreeNode parent = null;
        //找到要插入位置的父亲节点
        TreeNode cur = root;
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else {
                return false;
            }
        }
        //parent就是要插入位置的父亲节点
        TreeNode node = new TreeNode(val);
        if(parent.val > val) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }
    /**
     * 删除
     */
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                //找到key值的树，开始删除
                removeNode(cur, parent);
                break;
            }
        }
    }
    public void removeNode(TreeNode cur, TreeNode parent) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                //cur == parent.right
                parent.right = cur.right;
            }

        } else if (cur.right == null) {
            if(cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                //cur == parent.right
                parent.right = cur.left;
            }

        } else {
            //cur.left != null && cur.right != null
            //找到cur右子树的最小值，把这个最小值对cur进行覆盖，在删除这个最小值
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            //找到了右子树最小值target,target的值对cur的值进行覆盖
            cur.val = target.val;
            //一直往左走，target走到左边为空了，当前target就是最小值
            if(targetParent.left == target) {
                //删除target
                targetParent.left = target.right;
            }else {
                //targetParent.right == target
                //删除target
                targetParent.right = target.right;
            }
        }
    }
}
